package top.hmtools.services;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import top.hmtools.beans.ColumnsInfoBean;
import top.hmtools.common.TableNameTools;
import top.hmtools.system.SystemInfoTools;

@Service
public class BeanCreateService extends BaseService {

	private ApplicationContext applicationContext;
	
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		applicationContext=applicationContext;
	}

	public String createBean(String basePackage,String dbName,String tableName){
		StringBuffer result = new StringBuffer();
		boolean useCamelName = this.gmConfiguration.isUseCamelName();
		
		result.append("public class RegionBean implements Serializable{");
		result.append(SystemInfoTools.getOSLineSeparator());
		
		//获取所有表字段信息
		List<ColumnsInfoBean> allColumInfos = this.tablesInfoDao.getAllColumInfos(dbName, tableName);
				
		//生成字段
		for(ColumnsInfoBean column:allColumInfos){
			String beanName = "";
        	if(useCamelName){
        		beanName = TableNameTools.underlineToCamelByRegex(column.getColumn_name());
        	}else{
        		beanName = column.getColumn_name();
        	}
			result.append("/**");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("*"+column.getColumn_comment());
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("*/");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("protected String "+beanName+";");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("");
		}
		
		//生成 getter setter
		for(ColumnsInfoBean column:allColumInfos){
			String beanName = "";
			if(useCamelName){
				beanName = TableNameTools.underlineToCamelByRegex(column.getColumn_name());
			}else{
				beanName = column.getColumn_name();
			}
			result.append("/**");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("*获取"+column.getColumn_comment());
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("*/");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("public String get"+TableNameTools.upcaseFirst(beanName)+"(){");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("return this."+beanName+";");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("}");
			result.append(SystemInfoTools.getOSLineSeparator());

			result.append("/**");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("*设置"+column.getColumn_comment());
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("*/");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("public void set"+TableNameTools.upcaseFirst(beanName)+"(String "+beanName+"){");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("this."+beanName+"="+beanName+";");
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("}");
			result.append(SystemInfoTools.getOSLineSeparator());
		}
		result.append("");
		result.append("");
		result.append("");
		result.append("");
		result.append("");
		result.append("");
		result.append("");
		result.append("");
		result.append("}");
		
		return result.toString();
	}
}
